package com.genITeam.ria.bl;

import java.util.ArrayList;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.genITeam.ria.dao.SearchForumDAO;
import com.genITeam.ria.exception.NGFException;
import com.genITeam.ria.utility.HibernateUtils;
import com.genITeam.ria.utility.Utility;
import com.genITeam.ria.vo.Member;
import com.genITeam.ria.vo.Posts;
import com.genITeam.ria.vo.ResponseVo;

public class SearchForumBL {

	public SearchForumBL() {
		BasicConfigurator.configure();
	}

	Logger logger = Logger.getLogger(SearchForumBL.class);

	/**
	 * searchForum return the search Result data in XML format.
	 * 
	 * @param posts
	 * 
	 * @return String
	 * @throws NGFException
	 */

	public String searchForum(Posts posts) throws NGFException {
		Session session = null;
		Transaction tx = null;
		String searchForumXML = "";
		try {
			logger.info("Start getPersonalMessage");
			System.out.println("generateBuddiesData");
			SearchForumDAO searchForumDAO = new SearchForumDAO();
			session = HibernateUtils.getSessionFactory().getCurrentSession();
			tx = session.getTransaction();
			tx.begin();
			ResponseVo responseVo = searchForumDAO.searchForum(posts, session);
			ArrayList searchForumResult = responseVo.getSearchForumResult();

			searchForumXML = this.generateSearchForumXML(searchForumResult);

			tx.commit();

		} catch (Exception e) {
			System.out.println(e.toString());
			if (tx != null && tx.isActive()) {
				tx.rollback();
			} // end of if
			logger.error("Exception = " + e.toString());
			throw new NGFException(e.getMessage());

		}

		return searchForumXML;
	}

	/**
	 * generateSearchForumXML Generate the XML from the collection posts found
	 * in searchForumResult ArrayLsit
	 * 
	 * @param posts
	 * 
	 * @return String
	 * @throws NGFException
	 */

	public String generateSearchForumXML(ArrayList searchForumResult)
			throws NGFException {
		StringBuffer xml = new StringBuffer("");
		Posts posts = null;
		Member member = null;
		String postType = "";
		try {
			logger.info("Start generateSearchForumXML");
			xml = new StringBuffer("<resultset>\n");
			for (int i = 0; i < searchForumResult.size(); i++) {
				posts = (Posts) searchForumResult.get(i);
				member = (Member) posts.getMember();

				if (posts.getPostType() == 'F') {
					postType = "Forum";
				} else if (posts.getPostType() == 'T') {
					postType = "Thread";
				} else if (posts.getPostType() == 'P') {
					postType = "Message";
				}

				xml.append("<result>");
				xml.append("<postId>" + posts.getPostId() + "</postId>");
				xml.append("<title>" + Utility.getStringValue(posts.getTitle(),"") + "</title>");
				xml.append("<message>" + Utility.getStringValue(posts.getMessage(),"") + "</message>");
				xml.append("<postDate>" + posts.getPostDate() + "</postDate>");
				xml.append("<postType>" + postType + "</postType>");
				xml.append("<noOfView>" + posts.getNoOfView() + "</noOfView>");
				xml.append("<userName>" + Utility.getStringValue(member.getUserName(),"") + "</userName>");
				xml.append("</result>");
				xml.append("\n");

			}// end for

			xml.append("</resultset>");

			System.out.println(xml);

		} catch (Exception e) {
			System.out.println(e.toString());
			logger.error("Exception = " + e.toString());
			throw new NGFException(e.getMessage());
		}

		return xml.toString();
	}

	public static void main(String[] args) {

		try {
			SearchForumBL searchForumBL = new SearchForumBL();
			Posts posts = new Posts();
			posts.setTitle("irfan");
			posts.setMessage("");
			searchForumBL.searchForum(posts);

		} catch (Exception e) {

			System.out.println("Exception " + e.getMessage());
		}

	}
}
